﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using MaxLeaf.DB;
using System.Data;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace MaxLeaf.DAL.W2K
{



    public class goods_categoryGroupDAL : BaseGroupDAL
    {
        public goods_categoryGroupDAL()
            : base()
        {
            strK3TableName = "t_ComCategory";
            strWebTableName = "goods_category";

            strK3MapKeyFieldName = "FitemID";
            strWebMapKeyFieldName = "cat_id";
            strWebMapKeyTableName = "k3_goods_category";

            //strK3DestRealKeyFieldName = "FName";
            //strWebOrgDestRealKeyFieldName = "cat_name";

            strFItemClassID = Const.FItemClassID_Category;
            strLogType = LogType.goods_category.ToString();

            strFieldNames = "FNumber,FName";
        }

        public override int Update(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue)
        {
            int iDestKey = base.Update(dbOrgWebReadCon,dbOrgWebWriteCon, dbDestK3Con, strKeyValue);


            new goods_mainGroupDAL(Const.FItemClassID_ItemGroup_1).Update(dbDestK3Con, dbDestK3Con, dbDestK3Con, iDestKey.ToString());

            new goods_mainGroupDAL(Const.FItemClassID_ItemGroup_2).Update(dbDestK3Con, dbDestK3Con, dbDestK3Con, iDestKey.ToString());

            return iDestKey;
        }

        protected override void GetFNumber(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue, out string[] arrFnumber, out string[] arrFName, out string[] arrKeyValue)
        {

            KeyValuePair<string, IDataParameter[]> kvp = dbOrgWebReadCon.CreateCondParam(new Dictionary<string, object>() { { strWebMapKeyFieldName, strKeyValue } });
            DataRow drOrgData = dbOrgWebReadCon.GetDataFirstRow(string.Format("select parent_id,0+cat_level as cat_level,cat_name from goods_category where {0}", kvp.Key), kvp.Value);
            LogManager.Debug("cat_level " + CvtFunc.ToString(drOrgData["cat_level"]));
            arrFnumber = new string[CvtFunc.ToInt(drOrgData["cat_level"])];
            arrFName = new string[arrFnumber.Length];
            arrKeyValue = new string[arrFnumber.Length];

            arrFnumber[arrFnumber.Length - 1] = BaseFunctions.FormatWithZero(strKeyValue, 2);
            arrFName[arrFnumber.Length - 1] = CvtFunc.ToString(drOrgData["cat_name"]);
            arrKeyValue[arrFnumber.Length - 1] = strKeyValue;
            for (int i = arrFnumber.Length - 2; i >= 0; i--)
            {

                int intparent_id = CvtFunc.ToInt(drOrgData["parent_id"]);
                if (intparent_id > 0)
                {
                    arrFnumber[i] = BaseFunctions.FormatWithZero(intparent_id.ToString(), 2);
                    arrKeyValue[i] = intparent_id.ToString();

                    kvp = dbOrgWebReadCon.CreateCondParam(new Dictionary<string, object>() { { "cat_id", intparent_id.ToString() } });
                    drOrgData = dbOrgWebReadCon.GetDataFirstRow(string.Format("select parent_id,0+cat_level as cat_level,cat_name from goods_category where {0}", kvp.Key), kvp.Value);

                    arrFName[i] = CvtFunc.ToString(drOrgData["cat_name"]);
                }

            }
            string strFParentNumber = string.Empty;
            if (arrFnumber.Length > 1)
            {
                for (int i = 0; i < arrFnumber.Length - 1; i++)
                {
                    arrFnumber[i] = base.GetFShortNumber(dbOrgWebReadCon,dbOrgWebWriteCon, dbDestK3Con, arrKeyValue[i]);
                    strFParentNumber = string.Join(".", arrFnumber, 0, i + 1);
                }
            }
            arrFnumber[arrFnumber.Length - 1] = GetMaxFNumber(dbDestK3Con, strFParentNumber, Const.Len_Category);

        }
    }
}